package Dao.Impl;

import Dao.FinancialProductDAO;
import Model.FinancialProduct;
import Util.DbConnector;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class FinancialProductDAOImpl implements FinancialProductDAO {
    private static final Logger logger = LogManager.getLogger(FinancialProductDAOImpl.class);
    private final QueryRunner queryRunner = new QueryRunner();

    public FinancialProductDAOImpl() {
    }

    @Override
    public FinancialProduct getFinancialProductById(int productId) {
        String sql = "SELECT * FROM FinancialProducts WHERE id = ?";
        try (Connection connection = DbConnector.getConnection()) {
            return queryRunner.query(connection, sql, new BeanHandler<>(FinancialProduct.class), productId);
        } catch (SQLException e) {
            logger.error("获取理财产品失败，产品ID: " + productId, e);
            return null;
        }
    }

    @Override
    public void addFinancialProduct(FinancialProduct product) {
        String sql = "INSERT INTO FinancialProducts (name, description, interestRate, isActive) VALUES (?, ?, ?, ?)";
        try (Connection connection = DbConnector.getConnection()) {
            queryRunner.update(connection, sql,
                    product.getName(),
                    product.getDescription(),
                    product.getInterestRate(),
                    product.getIsActive());
        } catch (SQLException e) {
            logger.error("添加理财产品失败，产品名称: " + product.getName(), e);
        }
    }

    @Override
    public void updateFinancialProduct(FinancialProduct product) {
        String sql = "UPDATE FinancialProducts SET name = ?, description = ?, interestRate = ?, isActive = ? WHERE id = ?";
        try (Connection connection = DbConnector.getConnection()) {
            queryRunner.update(connection, sql,
                    product.getName(),
                    product.getDescription(),
                    product.getInterestRate(),
                    product.getIsActive(),
                    product.getId());
        } catch (SQLException e) {
            logger.error("更新理财产品失败，产品ID: " + product.getId(), e);
        }
    }

    @Override
    public List<FinancialProduct> getAllFinancialProducts() {
        String sql = "SELECT * FROM FinancialProducts";
        try (Connection connection = DbConnector.getConnection()) {
            return queryRunner.query(connection, sql, new BeanListHandler<>(FinancialProduct.class));
        } catch (SQLException e) {
            logger.error("获取所有理财产品失败", e);
            return null;
        }
    }

    @Override
    public void deleteFinancialProduct(int productId) {
        String sql = "DELETE FROM FinancialProducts WHERE id = ?";
        try (Connection connection = DbConnector.getConnection()) {
            queryRunner.update(connection, sql, productId);
        } catch (SQLException e) {
            logger.error("删除理财产品失败，产品ID: " + productId, e);
        }
    }
}